package com.diyunapp.xilinapp.adapters;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.bean.OrderGoodsInBean;
import com.diyunapp.xilinapp.myindent.ApplyForRefundActivity;
import com.diyunapp.xilinapp.myindent.TradeComplaintsaActivity;
import com.diyunapp.xilinapp.utils.FormatConfirm;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by NIDAYE on 2016/6/21.
 */
public class OrderInnerAdapter extends BaseAdapter {

    private List<OrderGoodsInBean> myLists;
    private Context myContext;
    private int clickType;
    private String  orderId;

    public OrderInnerAdapter(List<OrderGoodsInBean> myLists, Context myContext, int clickType, String orderId) {
        this.myLists = myLists;
        this.myContext = myContext;
        this.clickType = clickType;
        this.orderId = orderId;
    }

    @Override
    public int getCount() {
        return myLists.size();
    }

    @Override
    public Object getItem(int position) {
        return myLists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(myContext).inflate(R.layout.order_inner_list, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final OrderGoodsInBean data = myLists.get(position);

        Log.i("zhangxiaoxidebug", "goods info-------------position = " + position);
        ImageLoader.getInstance().displayImage(data.getGoodImg(), holder.orderconfirmGoodImg, FormatConfirm.getOption());
        holder.orderconfirmGoodDescriptionTv.setText(data.getGoodName());
        holder.orderconfirmGoodPriceTv.setText("￥" + data.getGoodPrice());

        holder.orderconfirmGoodNumbersTv.setText("×" + data.getTotalGoodNum() + "件");

        if (clickType <= 0) {
            holder.addtionClickLl.setVisibility(View.GONE);
        }
        else
        {
            holder.addtionClickLl.setVisibility(View.VISIBLE);
            if (clickType > 1)
            {
                holder.drawbackBt.setVisibility(View.VISIBLE);
                if(clickType>2)
                {
                    holder.drawbackBt.setText("退款退货中");
                }
                else
                {
                    holder.drawbackBt.setText("退款退货");
                }
            }
            else
            {
                holder.drawbackBt.setVisibility(View.GONE);
            }

            if (clickType > 0)
            {
                holder.tradeComplaintsBt.setVisibility(View.VISIBLE);
            }
            else
            {
                holder.tradeComplaintsBt.setVisibility(View.GONE);
            }
            if (clickType == 4)
            {
                holder.drawbackBt.setVisibility(View.GONE);
                holder.tradeComplaintsBt.setVisibility(View.GONE);
            }
        }

        holder.drawbackBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("zhangxiaoxidebug","orderId"+orderId);
                if(clickType>2) {
                    Toast.makeText(myContext,"您已申请了退款退货，查看进度请到“退款及退货”列表！",Toast.LENGTH_LONG).show();
                }
                else
                {
                    Intent drawbackInt = new Intent(myContext, ApplyForRefundActivity.class);
                    drawbackInt.putExtra("order_id", orderId);
                    drawbackInt.putExtra("gd_id", data.getGoodId());
                    myContext.startActivity(drawbackInt);
                }
            }
        });

        holder.tradeComplaintsBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("zhangxiaoxidebug","orderId"+orderId);
                Intent ComplaintInt = new Intent(myContext, TradeComplaintsaActivity.class);
                ComplaintInt.putExtra("order_id", orderId);
                ComplaintInt.putExtra("gd_id", data.getRecId());
                myContext.startActivity(ComplaintInt);
            }
        });

        return convertView;
    }

    static class ViewHolder {
        @Bind(R.id.orderconfirm_good_img)
        ImageView orderconfirmGoodImg;
        @Bind(R.id.orderconfirm_good_description_tv)
        TextView orderconfirmGoodDescriptionTv;
        @Bind(R.id.orderconfirm_good_price_tv)
        TextView orderconfirmGoodPriceTv;
        @Bind(R.id.orderconfirm_good_numbers_tv)
        TextView orderconfirmGoodNumbersTv;
        @Bind(R.id.drawback_bt)
        TextView drawbackBt;
        @Bind(R.id.trade_complaints_bt)
        TextView tradeComplaintsBt;
        @Bind(R.id.addtion_click_ll)
        LinearLayout addtionClickLl;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
